import java.util.PriorityQueue;
import java.util.Queue;

public class num1046 {
    public int lastStoneWeight(int[] stones) {
        //遍历数组把所有数都加到优先级队列中
        Queue<Ston> queue = new PriorityQueue<>();
        for (int i = 0; i < stones.length; i++) {
            queue.offer(new Ston(stones[i]));
        }
        //每次取出两个最大值，若他俩不相等就用大值减小值，把值返回队列
        while (!queue.isEmpty() && queue.size() != 1){
            int y = queue.poll().weight;
            int x = queue.poll().weight;
            if(x != y){
                int z = y - x;
                queue.offer(new Ston(z));
            }
        }
        if(queue.isEmpty()){
            return 0;
        }
        return queue.poll().weight;
    }
}
class Ston implements Comparable<Ston> {
    int weight;

    public Ston(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Ston o) {
        return o.weight - this.weight;
    }
}